home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Learn C++ (CodeWarrior) / Chap 07.02 - menu / menu.cp < prev    next >
Text File  |  1995-10-20  |  2KB  |  84 lines

  1. #include <iostream.h>
  2. #include <string.h>
  3.  
  4. const short        kMaxNameLength = 40;
  5.  
  6.  
  7. //---------------------------------------  MenuItem
  8.  
  9. class MenuItem
  10. {
  11.     private:
  12.         float        price;
  13.         char        name[ kMaxNameLength ];
  14.  
  15.     public:
  16.                     MenuItem( float itemPrice, char *itemName );
  17.         float        GetPrice();
  18.         float        operator+( MenuItem item );
  19.         float        operator+( float subtotal );
  20. };
  21.  
  22. MenuItem::MenuItem( float itemPrice, char *itemName )
  23. {
  24.     price = itemPrice;
  25.     strcpy( name, itemName );
  26. }
  27.  
  28. float    MenuItem::GetPrice()
  29. {
  30.     return( price );
  31. }
  32.  
  33. float    MenuItem::operator+( MenuItem item )
  34. {
  35.     cout << "MenuItem::operator+( MenuItem item )\n";
  36.     
  37.     return( GetPrice() + item.GetPrice() );
  38. }
  39.  
  40. float    MenuItem::operator+( float subtotal )
  41. {
  42.     cout << "MenuItem::operator+( float subtotal )\n";
  43.     
  44.     return( GetPrice() + subtotal );
  45. }
  46.  
  47.  
  48. float            operator+( float subtotal, MenuItem item );
  49. //    I added the previous line, cause CodeWarrior reports (correctly)
  50. //    that there was no prototype for float operator+().
  51. //    Now there IS a prototype! Comment out the line
  52. // if you want to see the warning message -- Dave Mark, 10/20/95
  53.  
  54.  
  55. //---------------------------------------  operator+()
  56.  
  57. float    operator+( float subtotal, MenuItem item )
  58. {
  59.     cout << "operator+( float subtotal, MenuItem item )\n";
  60.     
  61.     return( subtotal + item.GetPrice() );
  62. }
  63.  
  64.  
  65. //---------------------------------------  main()
  66.  
  67. int    main()
  68. {
  69.     MenuItem    chicken( 8.99, "Chicken Kiev with salad" );
  70.     MenuItem    houseWine( 2.99, "Riesling by the glass" );
  71.     MenuItem    applePie( 3.99, "Apple Pie a la Mode" );
  72.     float        total;
  73.     
  74.     total = chicken + houseWine + applePie;
  75.  
  76.     cout << "\nTotal: " << total
  77.         << "\n\n";
  78.     
  79.     total = chicken + (houseWine + applePie);
  80.  
  81.     cout << "\nTotal: " << total;
  82.         
  83.     return 0;
  84. }